package com.ruoyi.web.controller.vantController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.customer.CustomerInfo;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.service.customer.CustomerInfoService;
import com.ruoyi.system.service.customer.CustomerLoginService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/customer/logina")
public class ApiCustomerLonginController extends BaseController {

    @Autowired
    private CustomerInfoService customerInfoService;
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private CustomerLoginService customerLoginService;

    @GetMapping(value = "/findAllById")
    public AjaxResult getInfo()
    {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        int i = Integer.parseInt(String.valueOf(customerLogin.getCustomerId()));
        return AjaxResult.success(customerInfoService.findAllById(i));
    }

    @PostMapping(value = "/edit")
    public AjaxResult edit(@RequestBody CustomerInfo customerInfo)
    {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        CustomerInfo allById = customerInfoService.findAllById(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
        customerInfo.setCustomerInfId(allById.getCustomerInfId());
        return toAjax(customerInfoService.updateCustomerInfo(customerInfo));
    }

    @DeleteMapping("/remove")
    public AjaxResult remove()
    {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        CustomerInfo allById = customerInfoService.findAllById(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
        customerLoginService.deleteCustomerLoginById(Math.toIntExact(customerLogin.getCustomerId()));
        return toAjax(customerInfoService.deleteCustomerInfoById(allById.getCustomerInfId()));
    }

    @GetMapping(value = "/findCustListById")
    public AjaxResult findCustListById()
    {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return AjaxResult.success(customerLoginService.findCustListById(customerLogin.getCustomerId()));
    }


}
